package com.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.admin.mapper.SysDictDataMapper;
import com.cloud.admin.model.entity.SysDictData;
import com.cloud.admin.service.SysDictDataService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典数据表
 *
 * @author douhaichao code generator
 * @date 2019-07-24 18:12:52
 */
@Service
@AllArgsConstructor
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public SysDictData selectOne(SysDictData req) {
        QueryWrapper queryWrapper = new QueryWrapper<SysDictData>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<SysDictData> selectAll(SysDictData req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        queryWrapper.orderByAsc("dict_sort");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<SysDictData> selectPage(Page page, SysDictData req) {
        IPage<SysDictData> list = baseMapper.selectPage(
                page,
                new QueryWrapper<SysDictData>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<SysDictData> selectPageByLike(Page page, SysDictData req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getDictCode()) {
            queryWrapper.eq("dict_code", req.getDictCode());
        }

        if (null != req.getDictSort()) {
            queryWrapper.eq("dict_sort", req.getDictSort());
        }

        if (!StrUtil.isEmpty(req.getDictLabel())) {
            queryWrapper.like("dict_label", req.getDictLabel());
        }

        if (!StrUtil.isEmpty(req.getDictValue())) {
            queryWrapper.like("dict_value", req.getDictValue());
        }

        if (!StrUtil.isEmpty(req.getDictType())) {
            queryWrapper.like("dict_type", req.getDictType());
        }

        if (!StrUtil.isEmpty(req.getCssClass())) {
            queryWrapper.like("css_class", req.getCssClass());
        }

        if (!StrUtil.isEmpty(req.getListClass())) {
            queryWrapper.like("list_class", req.getListClass());
        }

        if (!StrUtil.isEmpty(req.getIsDefault())) {
            queryWrapper.like("is_default", req.getIsDefault());
        }

        if (!StrUtil.isEmpty(req.getStatus())) {
            queryWrapper.like("status", req.getStatus());
        }

        if (!StrUtil.isEmpty(req.getCreateBy())) {
            queryWrapper.like("create_by", req.getCreateBy());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }

        if (!StrUtil.isEmpty(req.getUpdateBy())) {
            queryWrapper.like("update_by", req.getUpdateBy());
        }

        if (null != req.getUpdateTime()) {
            queryWrapper.eq("update_time", req.getUpdateTime());
        }

        if (!StrUtil.isEmpty(req.getRemark())) {
            queryWrapper.like("remark", req.getRemark());
        }
        IPage<SysDictData> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(SysDictData req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
